package com.fs.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

import com.example.studentmanagersystem.MainActivity;
import com.fs.po.Stu;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class RegistUtil {
	
	public String registResult;
	private Context context;
	
	public RegistUtil(Context context) {
		 this.context=context;
	}
	
	/**
	 * 主界面登录数据提交到服务器
	 */
	public void userRegist(String nameKey,String passwordKey,String phoneKey,String sexKey,String ageKey,Stu stu,String url,String type) {
		RequestParams params = new RequestParams();
		params.addBodyParameter(nameKey, stu.getName());
		params.addBodyParameter(passwordKey, stu.getPassword());
		params.addBodyParameter(phoneKey, stu.getTel());
		params.addBodyParameter(sexKey, stu.getSex());
		params.addBodyParameter(ageKey, ""+stu.getAge());
		params.addBodyParameter("action", type);
		
		HttpUtils http = new HttpUtils();
		http.send(HttpMethod.POST, url, params, new RequestCallBack<String>() {

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				String Result=responseInfo.result;
				
				 Intent intent;
				if("success".equals(Result)){
					
					intent=new Intent(context, MainActivity.class);
					context.startActivity(intent);
					((Activity)context).finish();
					
				}else if("fail".equals(Result)){
					
					Toast.makeText(context, "注册失败", 0).show();
					
				}
				
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				System.out.println("失败");//【测试】
			}

		});
		
	}
	

	
}














